package cn.xshi.oauth.web;
import java.util.*;

import cn.xshi.common.base.*;
import cn.xshi.oauth.model.OauthFunctionInfo;
import cn.xshi.oauth.model.OauthResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.oauth.service.OauthResourcesService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.xshi.oauth.service.OauthFunctionInfoService;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageInfo;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
/**
 * @Desc 功能中心
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/oauthFunctionInfo")
@Api(value = "授权中心功能中心API",tags = "授权中心功能中心API",description = "授权中心功能中心API")
public class OauthFunctionInfoController extends BaseAction {

	@Resource
	private OauthFunctionInfoService oauthFunctionInfoService;

	@Resource
	private OauthResourcesService oauthResourcesService;

	/**
	* 查询功能列表并分页
	* @param baseSearch
	*/
	@PostMapping(value="/list")
	@ApiOperation(value="查询功能列表并分页", notes="查询功能列表并分页")
	@Auth(value = "/oauthFunctionInfo/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<OauthFunctionInfo>> getOauthFunctionInfoListByCondition(@RequestBody BaseSearch baseSearch){
		Map<String, Object> condition = baseSearch.convert();
		commonHPager(baseSearch);
		List<OauthFunctionInfo> oauthFunctionInfoList = oauthFunctionInfoService.getOauthFunctionInfoListByCondition(condition);
		PageInfo<OauthFunctionInfo> page = new PageInfo<OauthFunctionInfo>(oauthFunctionInfoList);
		return outPageBootStr(page,baseSearch);
	}

	/**
	* 查询单个功能
	* @param id
	*/
	@ApiOperation(value="查询单个功能", notes="查询单个功能")
	@GetMapping(value="/get/{id}")
	@Auth(value = "/oauthFunctionInfo/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult<OauthFunctionInfo> getOauthFunctionInfoById(@PathVariable("id")String id){
		OauthFunctionInfo oauthFunctionInfo = oauthFunctionInfoService.getOauthFunctionInfoById(id);
		return outDataStr(oauthFunctionInfo);
	}

	/**
	* 添加
	* @param oauthFunctionInfo 
	*/
	@ApiOperation(value="创建单个功能", notes="创建单个功能")
	@PostMapping(value="/add")
	public BaseResult addOauthFunctionInfo(@RequestBody OauthFunctionInfo oauthFunctionInfo){
		int i = 0;
		if(null != oauthFunctionInfo){
			i=oauthFunctionInfoService.addOauthFunctionInfo(oauthFunctionInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 修改
	* @param oauthFunctionInfo 
	*/
	@ApiOperation(value="编辑单个功能", notes="编辑单个功能")
	@PutMapping(value="/update")
	public BaseResult updateOauthFunctionInfo(@RequestBody OauthFunctionInfo oauthFunctionInfo){
		int i = 0;
		if(null != oauthFunctionInfo){
			i=oauthFunctionInfoService.updateOauthFunctionInfo(oauthFunctionInfo);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除功能", notes="删除功能")
	@DeleteMapping(value="/delete")
	public BaseResult delOauthFunctionInfo(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i=oauthFunctionInfoService.delOauthFunctionInfo(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}

	/**
	 * 功能资源树菜单
	 * @param sysModeId
	 * @param sysModuleId
	 * @return
	 */
	@ApiOperation(value="功能资源树菜单", notes="功能资源树菜单")
	@GetMapping(value="/treeList")
	@Auth(value = "/oauthFunctionInfo/treeList",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult treeList(String sysModeId,String sysModuleId){
		//1获取所有菜单
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("sysModuleId",sysModuleId);
		if(StringUtil.isEmpty(sysModeId)){
			condition.put("sysModeId","-1");
		}else {
			condition.put("sysModeId",sysModeId);
		}

		List<BaseBTreeGridEntity> list = new ArrayList<BaseBTreeGridEntity>();
		List<OauthFunctionInfo> oauthFunctionInfoList = oauthFunctionInfoService.getOauthFunctionInfoList(condition);
		List<OauthResources> oauthResourcesList = oauthResourcesService.getOauthResourcesListAll(condition);
		for(int j = 0; j < oauthResourcesList.size(); j++){
			OauthResources oauthResources = oauthResourcesList.get(j);
			BaseBTreeGridEntity baseBTreeGridEntity = new BaseBTreeGridEntity();
			baseBTreeGridEntity.setId(oauthResources.getId());
			baseBTreeGridEntity.setPid(oauthResources.getParentId());
			baseBTreeGridEntity.setText("<font style='font-family: cursive;font-size: initial;'>"+oauthResources.getName()+"</font>");
			baseBTreeGridEntity.setContent("");
			baseBTreeGridEntity.setVueIcon(oauthResources.getComponentIcon());
			baseBTreeGridEntity.setTempObject("Sources");
			list.add(baseBTreeGridEntity);
		}
		for(int i = 0; i < oauthFunctionInfoList.size(); i++){
			OauthFunctionInfo oauthFunctionInfo = oauthFunctionInfoList.get(i);
			BaseBTreeGridEntity baseBTreeGridEntity = new BaseBTreeGridEntity();
			baseBTreeGridEntity.setId(oauthFunctionInfo.getId());
			baseBTreeGridEntity.setPid(oauthFunctionInfo.getResourcesId());
			baseBTreeGridEntity.setText("<font color='#22b9ff' style='font-family: cursive; font-size: larger;'>"+oauthFunctionInfo.getName()+"</font>");
			baseBTreeGridEntity.setTempObject("Function");
			baseBTreeGridEntity.setContent(""+oauthFunctionInfo.getName());
			baseBTreeGridEntity.setIntegerappend(oauthFunctionInfo.getIsAuthority()+","+oauthFunctionInfo.getIsFilter());
//			if(("true").equals(expanded)){
//				BaseZTreeEntity.setExpanded(true);
//			}else{
//				BaseZTreeEntity.setExpanded(false);
//			}

			list.add(baseBTreeGridEntity);
		}
		return outStr(BaseBTreeGridEntity.buildTreeF(list));
	}

	/**
	 * 加载所有
	 * @param request
	 */
	@ApiOperation(value="查询全部功能", notes="查询全部功能")
	@GetMapping(value="/listAll")
	@Auth(value = "/oauthFunctionInfo/listAll",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BaseResult getOauthFunctionInfoList(HttpServletRequest request){
		//1获取所有菜单
		Map<String, Object> condition = new HashMap<String, Object>();
		String expanded = request.getParameter("expanded");
		String singleClickExpand = request.getParameter("singleClickExpand");
		List<BaseZTreeEntity> list = new ArrayList<BaseZTreeEntity>();
		List<OauthFunctionInfo> oauthFunctionInfoList = oauthFunctionInfoService.getOauthFunctionInfoList(condition);
		List<OauthResources> oauthResourcesList = oauthResourcesService.getOauthResourcesListAll(condition);
		for(int j = 0; j < oauthResourcesList.size(); j++){
			OauthResources oauthResources = oauthResourcesList.get(j);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(oauthResources.getId());
			BaseZTreeEntity.setPId(oauthResources.getParentId());
			BaseZTreeEntity.setText(oauthResources.getName());
			BaseZTreeEntity.setName(oauthResources.getName());
			BaseZTreeEntity.setContent("");
//			if("0".equals(oauthResources.getResources_leaf())){
//				BaseZTreeEntity.setHasLeaf(false);
//			}else{
//				BaseZTreeEntity.setHasLeaf(true);
//				//当菜单为末级时判断是否存在功能
//				if(hasLeaf(oauthFunctionInfoList, oauthResources.getResources_id())){
//					BaseZTreeEntity.setHasLeaf(false);
//				}else{
//					BaseZTreeEntity.setHasLeaf(true);
//				}
//			}
//			BaseZTreeEntity.setIcon("../deng/images/icons/target.png");
			BaseZTreeEntity.setTempObject("Sources");
			if(oauthResources.getParentId().equals("0")){
				//展开第一级菜单
				BaseZTreeEntity.setExpanded(true);
			}
//			if(("true").equals(expanded)){
//				BaseZTreeEntity.setExpanded(true);
//			}else{
//				BaseZTreeEntity.setExpanded(false);
//			}
			if("true".equals(singleClickExpand)){
				BaseZTreeEntity.setSingleClickExpand(true);
			}else{
				BaseZTreeEntity.setSingleClickExpand(false);
			}
			list.add(BaseZTreeEntity);
		}
		for(int i = 0; i < oauthFunctionInfoList.size(); i++){
			OauthFunctionInfo oauthFunctionInfo = oauthFunctionInfoList.get(i);
			BaseZTreeEntity BaseZTreeEntity = new BaseZTreeEntity();
			BaseZTreeEntity.setId(oauthFunctionInfo.getId());
			BaseZTreeEntity.setPId(oauthFunctionInfo.getResourcesId());
			BaseZTreeEntity.setText(oauthFunctionInfo.getName());
			BaseZTreeEntity.setName(oauthFunctionInfo.getName());
//			BaseZTreeEntity.setIcon("../deng/images/icons/target_point.png");
			BaseZTreeEntity.setTempObject("Function");
			BaseZTreeEntity.setContent(""+oauthFunctionInfo.getName());
			BaseZTreeEntity.setIntegerappend(oauthFunctionInfo.getIsAuthority()+","+oauthFunctionInfo.getIsFilter());
//			if(("true").equals(expanded)){
//				BaseZTreeEntity.setExpanded(true);
//			}else{
//				BaseZTreeEntity.setExpanded(false);
//			}
			BaseZTreeEntity.setExpanded(false);
			if("true".equals(singleClickExpand)){
				BaseZTreeEntity.setSingleClickExpand(true);
			}else{
				BaseZTreeEntity.setSingleClickExpand(false);
			}
			BaseZTreeEntity.setHasLeaf(true);
			list.add(BaseZTreeEntity);
		}
		BaseZTreeEntity baseZTreeEntity = new BaseZTreeEntity();
		List<BaseZTreeEntity> baseZTreeEntityList = baseZTreeEntity.buildTree(list,"0");
		String json = JsonUtil.toFastJson(baseZTreeEntityList);
		return outStr(json);
//		return outStr(BaseZTreeEntity.buildTree(list,false));
	}

	/**
	 * 判断菜单下面是否有功能
	 * @param oauthFunctionInfoList
	 * @param resourcesId
	 * @return
	 */
	public boolean hasLeaf(List<OauthFunctionInfo> oauthFunctionInfoList,String resourcesId){
		boolean flag = true;
		for(int i = 0; i < oauthFunctionInfoList.size(); i++){
			if(oauthFunctionInfoList.get(i).getResourcesId().equals(resourcesId)){
				return true;
			}
		}
		flag = false;
		return flag;
	}

	/**
	 * 导出功能
	 * @param response
	 * @param id
	 */
	@ApiOperation(value="导出功能", notes="导出功能")
	@GetMapping(value = "/export")
	public void exportOauthFunctionInfo(HttpServletResponse response, String id) {
		oauthFunctionInfoService.exportOauthFunctionInfo(response,id);
	}

	/**
	 * 导入功能
	 * @param request
	 * @return
	 */
	@ApiOperation(value="导入功能", notes="导入功能")
	@PostMapping(value = "/import")
	public BaseResult importOauthFunctionInfo(HttpServletRequest request,HttpServletResponse response) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;//转型为MultipartHttpRequest：
		MultiValueMap<String, MultipartFile> multipartFileMultiValueMap =  multipartRequest.getMultiFileMap();//获取所有文件
		Iterator<Map.Entry<String, List<MultipartFile>>> iterator = multipartFileMultiValueMap.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry<String, List<MultipartFile>> entry = iterator.next();
			for (MultipartFile multipartFile : entry.getValue()) {
				oauthFunctionInfoService.importOauthFunctionInfo(request,response,multipartFile);
			}
		}
		return BaseResult.success();
	}
}
